home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / navigator / browser.js next >
Encoding:
Text File  |  2002-05-17  |  12.1 KB  |  399 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Blake Ross <blakeross@telocity.com>
  24.  *   Peter Annema <disttsc@bart.nl>
  25.  *   Samir Gehani <sgehani@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
  42. var gPrintSettings = null;
  43. var gChromeState = null; // chrome state before we went into print preview
  44. var gOldCloseHandler = null; // close handler before we went into print preview
  45. var gInPrintPreviewMode = false;
  46.  
  47. function getWebNavigation()
  48. {
  49.   try {
  50.     return getBrowser().webNavigation;
  51.   } catch (e) {
  52.     return null;
  53.   }
  54. }
  55.  
  56. function BrowserReloadWithFlags(reloadFlags)
  57. {
  58.   /* First, we'll try to use the session history object to reload so 
  59.    * that framesets are handled properly. If we're in a special 
  60.    * window (such as view-source) that has no session history, fall 
  61.    * back on using the web navigation's reload method.
  62.    */
  63.  
  64.   var webNav = getWebNavigation();
  65.   try {
  66.     var sh = webNav.sessionHistory;
  67.     if (sh)
  68.       webNav = sh.QueryInterface(Components.interfaces.nsIWebNavigation);
  69.   } catch (e) {
  70.   }
  71.  
  72.   try {
  73.     webNav.reload(reloadFlags);
  74.   } catch (e) {
  75.   }
  76. }
  77.  
  78. function toggleAffectedChrome(aHide)
  79. {
  80.   // chrome to toggle includes:
  81.   //   (*) menubar
  82.   //   (*) navigation bar
  83.   //   (*) personal toolbar
  84.   //   (*) tab browser ``strip''
  85.   //   (*) sidebar
  86.  
  87.   if (!gChromeState)
  88.     gChromeState = new Object;
  89.   var chrome = new Array;
  90.   var i = 0;
  91.   chrome[i++] = document.getElementById("main-menubar");
  92.   chrome[i++] = document.getElementById("nav-bar");
  93.   chrome[i++] = document.getElementById("PersonalToolbar");
  94.  
  95.   // sidebar states map as follows:
  96.   //   was-hidden    => hide/show nothing
  97.   //   was-collapsed => hide/show only the splitter
  98.   //   was-shown     => hide/show the splitter and the box
  99.   if (aHide)
  100.   {
  101.     // going into print preview mode
  102.     if (sidebar_is_collapsed())
  103.     {
  104.       gChromeState.sidebar = "was-collapsed";
  105.       chrome[i++] = document.getElementById("sidebar-splitter");
  106.     }
  107.     else if (sidebar_is_hidden())
  108.       gChromeState.sidebar = "was-hidden";
  109.     else 
  110.     {
  111.       gChromeState.sidebar = "was-visible";
  112.       chrome[i++] = document.getElementById("sidebar-box");
  113.       chrome[i++] = document.getElementById("sidebar-splitter");
  114.     }
  115.   }
  116.   else
  117.   {
  118.     // restoring normal mode (i.e., leaving print preview mode)
  119.     if (gChromeState.sidebar == "was-collapsed" ||
  120.         gChromeState.sidebar == "was-visible")
  121.       chrome[i++] = document.getElementById("sidebar-splitter");
  122.     if (gChromeState.sidebar == "was-visible")
  123.       chrome[i++] = document.getElementById("sidebar-box");
  124.   }
  125.  
  126.   // now that we've figured out which elements we're interested, toggle 'em
  127.   for (i = 0; i < chrome.length; ++i)
  128.   {
  129.     if (aHide)
  130.       chrome[i].hidden = true;
  131.     else
  132.       chrome[i].hidden = false;
  133.   }
  134.  
  135.   // if we are unhiding and sidebar used to be there rebuild it
  136.   if (!aHide && gChromeState.sidebar == "was-visible")
  137.     SidebarRebuild();
  138.     
  139.   // now deal with the tab browser ``strip'' 
  140.   var theTabbrowser = document.getElementById("content"); 
  141.   if (aHide) // normal mode -> print preview
  142.   {
  143.     gChromeState.hadTabStrip = theTabbrowser.getStripVisibility();
  144.     theTabbrowser.setStripVisibilityTo(false);
  145.   }
  146.   else // print preview -> normal mode
  147.   {
  148.     // tabs were showing before entering print preview
  149.     if (gChromeState.hadTabStrip) 
  150.     {
  151.       theTabbrowser.setStripVisibilityTo(true);
  152.       gChromeState.hadTabStrip = false; // reset
  153.     }
  154.   }
  155. }
  156.  
  157. function showPrintPreviewToolbar()
  158. {
  159.   toggleAffectedChrome(true);
  160.   const kXULNS = 
  161.     "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  162.  
  163.   var printPreviewTB = document.createElementNS(kXULNS, "toolbar");
  164.   printPreviewTB.setAttribute("printpreview", true);
  165.   printPreviewTB.setAttribute("id", "print-preview-toolbar");
  166.  
  167.   var navTB = document.getElementById("nav-bar");
  168.   navTB.parentNode.appendChild(printPreviewTB);
  169. }
  170.  
  171. function BrowserExitPrintPreview()
  172. {
  173.   gInPrintPreviewMode = false;
  174.  
  175.   var browser = getBrowser();
  176.   browser.setAttribute("handleCtrlPageUpDown", "true");
  177.  
  178.   // exit print preview galley mode in content area
  179.   var ifreq = _content.QueryInterface(
  180.     Components.interfaces.nsIInterfaceRequestor);
  181.   var webBrowserPrint = ifreq.getInterface(
  182.     Components.interfaces.nsIWebBrowserPrint);     
  183.   webBrowserPrint.exitPrintPreview(); 
  184.   _content.focus();
  185.  
  186.   // remove the print preview toolbar
  187.   var navTB = document.getElementById("nav-bar");
  188.   var printPreviewTB = document.getElementById("print-preview-toolbar");
  189.   navTB.parentNode.removeChild(printPreviewTB);
  190.  
  191.   // restore chrome to original state
  192.   toggleAffectedChrome(false);
  193.  
  194.   // restore old onclose handler if we found one before previewing
  195.   var mainWin = document.getElementById("main-window");
  196.   mainWin.setAttribute("onclose", gOldCloseHandler);
  197. }
  198.  
  199. function GetPrintSettings(webBrowserPrint)
  200. {
  201.   var prevPS = gPrintSettings;
  202.  
  203.   try {
  204.     if (gPrintSettings == null) {
  205.       var useGlobalPrintSettings = true;
  206.       var pref = Components.classes["@mozilla.org/preferences-service;1"]
  207.                            .getService(Components.interfaces.nsIPrefBranch);
  208.       if (pref) {
  209.         useGlobalPrintSettings = pref.getBoolPref("print.use_global_printsettings", false);
  210.       }
  211.  
  212.       if (useGlobalPrintSettings) {
  213.         gPrintSettings = webBrowserPrint.globalPrintSettings;
  214.       } else {
  215.         gPrintSettings = webBrowserPrint.newPrintSettings;
  216.       }
  217.     }
  218.   } catch (e) {
  219.     dump("GetPrintSettings "+e);
  220.   }
  221.  
  222.   return gPrintSettings;
  223. }
  224.  
  225. function BrowserPrintPreview()
  226. {
  227.   gInPrintPreviewMode = true;
  228.  
  229.   var browser = getBrowser();
  230.   browser.setAttribute("handleCtrlPageUpDown", "false");
  231.  
  232.   var mainWin = document.getElementById("main-window");
  233.  
  234.   // save previous close handler to restoreon exiting print preview mode
  235.   if (mainWin.hasAttribute("onclose"))
  236.     gOldCloseHandler = mainWin.getAttribute("onclose");
  237.   else
  238.     gOldCloseHandler = null;
  239.   mainWin.setAttribute("onclose", "BrowserExitPrintPreview(); return false;");
  240.  
  241.   try {
  242.     var ifreq = _content.QueryInterface(
  243.       Components.interfaces.nsIInterfaceRequestor);
  244.     var webBrowserPrint = ifreq.getInterface(
  245.       Components.interfaces.nsIWebBrowserPrint);     
  246.     if (webBrowserPrint) {
  247.       gPrintSettings = GetPrintSettings(webBrowserPrint);
  248.       webBrowserPrint.printPreview(gPrintSettings);
  249.     }
  250.  
  251.     // show the toolbar after we go into print preview mode so
  252.     // that we can initialize the toolbar with total num pages
  253.     showPrintPreviewToolbar();
  254.  
  255.     _content.focus();
  256.   } catch (e) {
  257.     // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  258.     // causing an exception to be thrown which we catch here.
  259.     // Unfortunately this will also consume helpful failures, so add a
  260.     // dump(e); // if you need to debug
  261.   }
  262. }
  263.  
  264.  
  265. function BrowserPrintSetup()
  266. {
  267.   var didOK = false;
  268.   try {
  269.     var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  270.     var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);     
  271.     if (webBrowserPrint) {
  272.       gPrintSettings = GetPrintSettings(webBrowserPrint);
  273.     }
  274.  
  275.     didOK = goPageSetup(window, gPrintSettings);  // from utilityOverlay.js
  276.     if (didOK) {
  277.       if (webBrowserPrint) {
  278.         if (webBrowserPrint.doingPrintPreview) {
  279.           webBrowserPrint.printPreview(gPrintSettings);
  280.         }
  281.       }
  282.     }
  283.  
  284.   } catch (e) {
  285.     dump("BrowserPrintSetup "+e);
  286.   }
  287.   return didOK;
  288. }
  289.  
  290. function BrowserPrint()
  291. {
  292.   try {
  293.     var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  294.     var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);     
  295.     if (webBrowserPrint) {
  296.       gPrintSettings = GetPrintSettings(webBrowserPrint);
  297.       webBrowserPrint.print(gPrintSettings, null);
  298.     }
  299.   } catch (e) {
  300.     // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  301.     // causing an exception to be thrown which we catch here.
  302.     // Unfortunately this will also consume helpful failures, so add a
  303.     // dump(e); // if you need to debug
  304.   }
  305. }
  306.  
  307. function BrowserSetDefaultCharacterSet(aCharset)
  308. {
  309.   // no longer needed; set when setting Force; see bug 79608
  310. }
  311.  
  312. function BrowserSetForcedCharacterSet(aCharset)
  313. {
  314.   var docCharset = getBrowser().docShell.QueryInterface(
  315.                             Components.interfaces.nsIDocCharset);
  316.   docCharset.charset = aCharset;
  317.   BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
  318. }
  319.  
  320. function BrowserSetForcedDetector()
  321. {
  322.   getBrowser().documentCharsetInfo.forcedDetector = true;
  323. }
  324.  
  325. function BrowserFind()
  326. {
  327.   var focusedWindow = document.commandDispatcher.focusedWindow;
  328.   if (!focusedWindow || focusedWindow == window)
  329.     focusedWindow = window._content;
  330.  
  331.   findInPage(getBrowser(), window._content, focusedWindow)
  332. }
  333.  
  334. function BrowserFindAgain()
  335. {
  336.     var focusedWindow = document.commandDispatcher.focusedWindow;
  337.     if (!focusedWindow || focusedWindow == window)
  338.       focusedWindow = window._content;
  339.  
  340.   findAgainInPage(getBrowser(), window._content, focusedWindow)
  341. }
  342.  
  343. function BrowserCanFindAgain()
  344. {
  345.   return canFindAgainInPage();
  346. }
  347.  
  348. function getMarkupDocumentViewer()
  349. {
  350.   return getBrowser().markupDocumentViewer;
  351. }
  352.  
  353. /**
  354.  * Content area tooltip.
  355.  * XXX - this must move into XBL binding/equiv! Do not want to pollute
  356.  *       navigator.js with functionality that can be encapsulated into
  357.  *       browser widget. TEMPORARY!
  358.  *
  359.  * NOTE: Any changes to this routine need to be mirrored in ChromeListener::FindTitleText()
  360.  *       (located in mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp)
  361.  *       which performs the same function, but for embedded clients that
  362.  *       don't use a XUL/JS layer. It is important that the logic of
  363.  *       these two routines be kept more or less in sync.
  364.  *       (pinkerton)
  365.  **/
  366. function FillInHTMLTooltip(tipElement)
  367. {
  368.   var retVal = false;
  369.   if (tipElement.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
  370.     return retVal;
  371.  
  372.   const XLinkNS = "http://www.w3.org/1999/xlink";
  373.  
  374.  
  375.   var titleText = null;
  376.   var XLinkTitleText = null;
  377.   
  378.   while (!titleText && !XLinkTitleText && tipElement) {
  379.     if (tipElement.nodeType == Node.ELEMENT_NODE) {
  380.       titleText = tipElement.getAttribute("title");
  381.       XLinkTitleText = tipElement.getAttributeNS(XLinkNS, "title");
  382.     }
  383.     tipElement = tipElement.parentNode;
  384.   }
  385.  
  386.   var texts = [titleText, XLinkTitleText];
  387.   var tipNode = document.getElementById("aHTMLTooltip");
  388.  
  389.   for (var i = 0; i < texts.length; ++i) {
  390.     var t = texts[i];
  391.     if (t && t.search(/\S/) >= 0) {
  392.       tipNode.setAttribute("label", t);
  393.       retVal = true;
  394.     }
  395.   }
  396.  
  397.   return retVal;
  398. }
  399.